package com.newtouch.bxzs.business.personal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.enclosure.mapper.AttachmentMapper;
import com.newtouch.bxzs.business.message.service.AnnouncementService;
import com.newtouch.bxzs.business.personal.controller.PartnersController;
import com.newtouch.bxzs.business.personal.mapper.CooperationCompanyUserMapper;
import com.newtouch.bxzs.business.personal.mapper.PartnersMapper;
import com.newtouch.bxzs.business.personal.model.PartnersBO;
import com.newtouch.bxzs.business.personal.model.PartnersDetailBO;
import com.newtouch.bxzs.business.personal.service.PartnersService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.DistributionTypeEnum;
import com.newtouch.bxzs.common.base.em.SystemParamsEnum;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * 合作伙伴Service的实现类
 *
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/18 15:21</td>
 * <td>田尚柱</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author 田尚柱
 * @version 1.0.0
 */

@Service
public class PartnersServiceImpl implements PartnersService {
    @Resource
    private CooperationCompanyUserMapper cooperationCompanyUserMapper;

    @Resource
    private PartnersMapper partnersMapper;

    @Resource
    private AttachmentMapper attachmentMapper;

    @Resource
    private AnnouncementService announcementService;

    @Autowired
    RedisUtils redisUtils;

    /**
     * 静态变量：日志
     */
    private static final Log logger = LogFactory.getLog(PartnersController.class);

    /**
     * (non-Javadoc)
     *
     * @see com.newtouch.bxzs.business.personal.service.PartnersService#getPartnersList(java.lang.Integer,java.lang.Integer)
     */
    @Override
    public IPage<PartnersBO> getPartnersList(Integer currentPage, Integer tagFlag) {
        /* 拿到当前登录用户的工号*/
        String userJobNumber = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserJobNumber();
        if (StringUtils.isEmpty(userJobNumber)) {
            logger.info("用户工号为空");
            throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
        }
        // 封装分页参数
        Page<PartnersBO> page = new Page<>(currentPage, 10);
        //通过缓存拿到合作伙伴查询日期（年）
        String partnersSelectYear = announcementService.getParameterValueRedis(SystemParamsEnum.PARTNERS_SELECT_YEAR.getCode());
        int year = Integer.parseInt(partnersSelectYear);
        Date endTime = new Date();
        DateTime startTime = DateUtil.offsetDay(endTime, -year * 365);
        /* 通过登录用户的工号查询出对应的合作伙伴列表*/
        //银行端(这里没有与前端统一，2是银行)
        if (DistributionTypeEnum.INSURANCE.getCode().equals(redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE.getCode()))){
            return partnersMapper.getPartnersList(userJobNumber, page, startTime, endTime);
        } else {
            return partnersMapper.getPartnersListBx(userJobNumber, page, startTime, endTime);
        }
    }

    /**
     * (non-Javadoc)
     *
     * @see PartnersService#getPartnersDetail(String, Integer)
     */
    @Override
    public PartnersDetailBO getPartnersDetail(String cooperationCompanyUserId, Integer tagFlag) {
        if (StringUtils.isEmpty(cooperationCompanyUserId)) {
            logger.error("合作公司用户id为空");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        PartnersDetailBO partnersDetailBO = new PartnersDetailBO();
        /* 使用工具直接进行实体与BO的复制转换，前面是来源，后面是目标*/
        BeanUtil.copyProperties(cooperationCompanyUserMapper.selectById(cooperationCompanyUserId), partnersDetailBO);
        String responseTime = null;
        //银行端(这里没有与前端统一，2是银行)
        if (DistributionTypeEnum.INSURANCE.getCode().equals(redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE.getCode()))){
            responseTime = partnersMapper.getResponseTimeBx(cooperationCompanyUserId);
        }else {
            responseTime = partnersMapper.getResponseTime(cooperationCompanyUserId);
        }
        partnersDetailBO.setResponseTime(responseTime+"分钟");

        return partnersDetailBO;
    }
}
